package chapter8.SmallChanageSys;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * 1.该类是完成零钱通的各个功能的类
 * 2.使用面向对象方法
 * 3.每个功能对应一个方法
 */


public class SmallChangeSysOOp {
    boolean loop = true;
    String detail = "";//用来看零钱明细
    double money = 0;//收益金额
    Date date = null;//日期类
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd HH:mm");
    String local = "";
    double comsume = 0;
    double blance = 0;

    public void mainMenu() {
        //显示主菜单
        System.out.println("显示零钱通菜单");
        do {
            System.out.println("-----零钱通------");
            System.out.println("\t\t1.零钱通明细");
            System.out.println("\t\t2.收益入账");
            System.out.println("\t\t3.消费");
            System.out.println("\t\t4.退\t出");
            System.out.println("请选择1-4");
            Scanner scanner = new Scanner(System.in);
            int mune = scanner.nextInt();
            switch (mune) {
                case 1: {
                    this.detail();
                    break;
                }
                case 2:
                    this.income();
                    break;
                case 3:
                    this.pay();
                    break;
                case 4:
                    this.exit();
                    break;
                default:
                    System.out.println("输入有误请重新输入");
            }
        } while (loop);
    }

    //明细
    public void detail() {
        System.out.println("----零钱明细----");
        System.out.println(detail);
    }

    //完成收益入账
    public void income() {
        System.out.println("请输入金额");
        Scanner scanner1 = new Scanner(System.in);
        money = scanner1.nextDouble();
        //也可以选择不正确的条件进行判断直接break
        if (money <= 0) {
            System.out.println("您输入的金额有误，请重新输入");
            return;
        }
        blance += money;
        date = new Date();//获取到当前时间
        detail += "\n收益入账\t" + "+" + money + "\t" + simpleDateFormat.format(date) + "\t" + blance;
        return;
    }

    //消费方法
    public void pay() {
        System.out.println("请输入消费金额");
        Scanner scanner1 = new Scanner(System.in);
        comsume = scanner1.nextDouble();
        System.out.println("请输入消费地点");
        local = scanner1.next();
        //提前对余额进行判断
        //可以选择不对的输入进行判断，然后break;?
        if ((blance -= comsume) >= 0) {
            detail += "\n" + local + "\t" + "-" + comsume + "\t" + simpleDateFormat.format(date) + "\t" + blance;
            return;
        } else {
            System.out.println("您的余额不足");
            return;
        }
    }

    public void exit() {
        String choice = "";
        while (true) {
            System.out.println("你真的要退出吗Y/N");
            Scanner scanner1 = new Scanner(System.in);
            choice = scanner1.next();
            if (choice.equals("Y") || choice.equals("N")) {
                break;
            }
        }
        //当退出while循环后，在对choice进行判断
        //建议一段代码，完成一个小功能，尽量不要混在一起（低耦合）
        if (choice.equals("Y")) {
            loop = false;
        }
        return;
    }
}
